//
//  DFGridImageView.m
//  DFTimelineView
//
//  Created by Allen Zhong on 15/9/27.
//  Copyright (c) 2015年 Datafans, Inc. All rights reserved.
//

#import "DFGridImageView.h"
#import "UIImageView+WebCache.h"
#import "DFBigImageView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#import "TopicleWebViewController.h"
#define Padding 5
#define BigImgHeight 200//180
#define BigImgWidth SCREEN_WIDTH-30
#define OneImageMaxWidth [UIScreen mainScreen].bounds.size.width*0.5

@interface DFGridImageView()


@property (nonatomic, strong) NSMutableArray *images;

@property (nonatomic, strong) NSMutableArray *srcImages;

@property (nonatomic, strong) NSMutableArray *imageViews;
@property (nonatomic, strong) DFBigImageView *bigImgView;
@property (nonatomic, strong) DFBigImageView *smallImgView1;
@property (nonatomic, strong) DFBigImageView *smallImgView2;
@property (nonatomic, strong) DFBigImageView *smallImgView3;
@property (nonatomic, strong) DFBigImageView *smallImgView4;



@end

@implementation DFGridImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _imageViews = [NSMutableArray array];
        
        [self initView];
    }
    return self;
}

-(void) initView
{
    
    float x = 0;
    float y = 0;
    float width = BigImgWidth;
    float height = BigImgHeight;
    _bigImgView = [[DFBigImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:_bigImgView];
    [_bigImgView.imageButton addTarget:self action:@selector(tapImageViewAction:) forControlEvents:UIControlEventTouchUpInside];
    self.bigImgView.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.bigImgView.imageView.layer.masksToBounds = YES;
         x = CGRectGetMinX(self.bigImgView.frame);
         y = CGRectGetMaxY(self.bigImgView.frame)+Padding;
         width = (BigImgWidth-3*Padding)/4;
         height = width;
       _smallImgView1 = [[DFBigImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
        _smallImgView1.imageButton.tag = 1;
    _smallImgView1.hidden = YES;
     [_smallImgView1.imageButton addTarget:self action:@selector(tapImageView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_smallImgView1];
    
    x = CGRectGetMaxX(self.smallImgView1.frame)+Padding;
    _smallImgView2 = [[DFBigImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    _smallImgView2.hidden = YES;
    _smallImgView2.imageButton.tag = 2;
     [_smallImgView2.imageButton addTarget:self action:@selector(tapImageView:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_smallImgView2];
    
    x = CGRectGetMaxX(self.smallImgView2.frame)+Padding;
    _smallImgView3 = [[DFBigImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    _smallImgView3.hidden = YES;
    _smallImgView3.imageButton.tag = 3;
     [_smallImgView3.imageButton addTarget:self action:@selector(tapImageView:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_smallImgView3];
    x = CGRectGetMaxX(self.smallImgView3.frame)+Padding;
    _smallImgView4 = [[DFBigImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    _smallImgView4.hidden = YES;
    _smallImgView4.imageButton.tag = 4;
     [_smallImgView4.imageButton addTarget:self action:@selector(tapImageView:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_smallImgView4];


}
-(void)updateWithSrcImages:(NSMutableArray *)srcImages oneImageWidth:(CGFloat)oneImageWidth oneImageHeight:(CGFloat)oneImageHeight
{
   
    self.srcImages = [NSMutableArray new];
    for (NSString *tmpString in srcImages) {
        
            int width = (BigImgWidth-3*Padding)/4;
           NSString *newString = [NSString stringWithFormat:@"%@?imageView2/0/w/%d/h/%d",tmpString,width,width];
            [self.srcImages addObject:newString];
    }
    
    self.smallImgView1.hidden = YES;
    self.smallImgView2.hidden = YES;
    self.smallImgView3.hidden = YES;
    self.smallImgView4.hidden = YES;
    if (self.srcImages.count>0) {
        NSString *str = [srcImages objectAtIndex:0];
       [self.bigImgView.imageView sd_setImageWithURL:[NSURL URLWithString:str] width:BigImgWidth height:0];
        
        if (self.srcImages.count<=5) {
            switch (self.srcImages.count) {
                    
                case 5:
                {
                    [_smallImgView4.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:4]] placeholderImage:[UIImage imageNamed:@""]];
                    _smallImgView4.hidden = NO;
                }
                case 4:
                {
                    [_smallImgView3.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:3]] placeholderImage:[UIImage imageNamed:@""]];
                    _smallImgView3.hidden = NO;
                }
                case 3:
                {
                    [_smallImgView2.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:2]] placeholderImage:[UIImage imageNamed:@""]];
                    _smallImgView2.hidden = NO;
                    
                }
                case 2:
                {
                    [_smallImgView1.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@""]];
                    _smallImgView1.hidden = NO;
                }
                    break;
                default:
                    break;
            }
        }else{
        
            //图片大于5张得情况
//        case 5:
//            {
                [_smallImgView4.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:4]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                _smallImgView4.hidden = NO;
//            }
//        case 4:
//            {
                [_smallImgView3.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:3]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                _smallImgView3.hidden = NO;
//            }
//        case 3:
//            {
                [_smallImgView2.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:2]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                _smallImgView2.hidden = NO;
                
//            }
//        case 2:
//            {
                [_smallImgView1.imageView sd_setImageWithURL:[NSURL URLWithString:[self.srcImages objectAtIndex:1]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                _smallImgView1.hidden = NO;
//            }
        
        }
       

    }
    [self.srcImages removeAllObjects];
    self.srcImages = srcImages;
}


- (void)tapImageViewAction:(DFBigImageView *)sender
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClickBigImg)]) {
    
        [_delegate onClickBigImg];
    }
    
}

-(void)tapImageView:(DFBigImageView *)sender
{
   
    NSLog(@"tag: %ld", (long)sender.tag);
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    
    NSMutableArray *photos = [NSMutableArray array];
    
    if (self.srcImages.count >= 2) {
        
        for (int i=0; i<self.srcImages.count; i++) {
            MJPhoto *photo = [[MJPhoto alloc] init];
            
            id img = [self.srcImages objectAtIndex:i];
            if ([img isKindOfClass:[UIImage class]]) {
                photo.image = img;
            }else{
                photo.url = [NSURL URLWithString:[self.srcImages objectAtIndex:i]];
            }

            [photos addObject:photo];
        }
        
    }else{
        MJPhoto *photo = [[MJPhoto alloc] init];
        id img = [self.srcImages objectAtIndex:0];
        if ([img isKindOfClass:[UIImage class]]) {
            photo.image = img;
        }else{
            photo.url = [NSURL URLWithString:[self.srcImages objectAtIndex:0]];
        }
        
        [photos addObject:photo];
    }
    
    browser.photos = photos;
    browser.currentPhotoIndex = sender.tag;
    
    [browser show];
}


+(CGFloat)getHeight:(NSMutableArray *)images maxWidth:(CGFloat)maxWidth oneImageWidth:(CGFloat)oneImageWidth oneImageHeight:(CGFloat)oneImageHeight

{
    CGFloat height = 0;
    if (images.count>=2) {
         height= BigImgHeight+(BigImgWidth-3*Padding)/4+ Padding;
    }else{
         height= BigImgHeight + Padding;
    }
    return height;
}


- (void)touchUpInside:(id)sender
{
    NSLog(@"%@", sender);
}

@end
